package com.stox.ui.javafx.charting.model.uiunit;

import java.util.ArrayList;
import java.util.List;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.shape.StrokeLineJoin;

import com.stox.dto.AbstractBar;
import com.stox.ui.javafx.charting.model.plot.BarPrimaryPlot;
import com.stox.ui.javafx.charting.widget.PrimaryChart;
import com.stox.util.Constant;

public class BarPrimaryUiUnit extends AbstractPrimaryUiUnit<AbstractBar, 
																					PrimaryChart<BarPrimaryPlot>,
																					BarPrimaryUiUnit, 
																					BarPrimaryPlot> {
	
	private double open;
	
	private double high;
	
	private double low;
	
	private double close;
	
	public BarPrimaryUiUnit(AbstractBar model, BarPrimaryPlot plot, int index) {
		super(model, plot, index);
	}
	
	@Override
	public void update() {
		super.update();
		
		if(null != getPlot().getBackgroundColorStrategy())
			getPlot().getBackgroundColorStrategy().apply(this);
		
		if(null != getPlot().getColorStrategy())
			getPlot().getColorStrategy().apply(this);

		open = height(getModel().getOpen());
		high = height(getModel().getHigh());
		low = height(getModel().getLow());
		close = height(getModel().getClose());
	}
	
	@Override
	protected void updateWidth() {
		if(getPlot() == getPlot().getChart().getChartingView().getPrimaryChart().getFrontPlot()) {
			super.updateWidth(getModel().getVolume());
		}else{
			super.updateWidth();
		}
	}
	
	@Override
	public List<String> info() {
		List<String> info = new ArrayList<String>();
		info.add("Date : "+Constant.dateFormat.format(getModel().getDate()));
		info.add("Open : "+getModel().getOpen());
		info.add("High : "+getModel().getHigh());
		info.add("Low : "+getModel().getLow());
		info.add("Close : "+getModel().getClose());
		info.add("Volume : "+getModel().getVolume());
		return info;
	}
	
	@SuppressWarnings("incomplete-switch")
	@Override
	public void paint() {
		GraphicsContext gc = getPlot().getChart().getCanvas().getGraphicsContext2D();
		gc.setLineCap(StrokeLineCap.ROUND);
		gc.setLineJoin(StrokeLineJoin.ROUND);
		double lineWidth = Math.max(width/5,1);
		gc.setLineWidth(lineWidth);
		switch(getPlot().getChartType()) {
		case CANDLESTICK : 
			gc.setStroke(Color.WHITE);
			gc.strokeLine(x + width/2, high, x + width/2, low);
			gc.setFill(color);
			gc.fillRect(x + width/8, Math.min(open, close), width*3/4, Math.abs(open - close));
			break;
		case HLC :
			gc.setStroke(color);
			double xx = x + width/8 + lineWidth/2;
			gc.strokeLine(xx, high, xx, low);
			gc.strokeLine(xx, close, x + width*3/4, close);
			break;
		case OHLC : 
			gc.setStroke(color);
			xx = x + width/2;
			gc.strokeLine(xx, high, xx, low);
			gc.strokeLine(xx, close, x + width*3/4, close);
			gc.strokeLine(xx, open, x + width/8 + lineWidth/2, open);
			break;
		case LINE : 
			if(index != getPlot().getUiUnits().size()-1) {
				gc.setStroke(color);
				gc.strokeLine(x + width, close, x, getPlot().getUiUnits().get(index+1).close);
			}
		}
		
	}
	
}